require "Common/define"
require "Battle/Manager/ShaderEffectManager"

EntityEffect = {};
EntityEffect.__index = EntityEffect;

function EntityEffect:new(roleGo, fightId)
    local object ={};

    object.roleGo = roleGo;
    object.fightId = fightId;

    setmetatable(object, EntityEffect);
    return object;
end


function EntityEffect:startFlashEffect()
    if self.flashEffect then
        return
    end

    self.flashEffect = true;
    self.flashTime = 0;
    self.flashCount = 0;
end


function EntityEffect:updateFlashEffect(dt)

   --logMy('updateFlashEffect  ', self.fightId, self.mbp == nil, self.renderer == nil);

    if (not self.flashEffect) or (not self.mbp) or (not self.renderer) then
        return
    end

    if self.flashCount >= ShaderEffectManager.getHitFlashCountMax() then
        --    end
        self.mbp:SetFloat('_FlashTime', 1);--original color
        self.flashEffect = false;

    else
        self.flashTime = self.flashTime + dt;
        if self.flashTime > ShaderEffectManager.getHitFlashInterval() then
            self.flashTime = 0;
            self.flashCount = self.flashCount + 1;

            if self.flashCount % 2 == 1 then
                self.mbp:SetFloat('_FlashTime', 0);--白色
            else
                self.mbp:SetFloat('_FlashTime', 1);--original color
            end
        end
    end

    self.mbp:SetColor('_HitFlashColor', ShaderEffectManager.getHitFlashColor());

    self.renderer:SetPropertyBlock(self.mbp);
end




function EntityEffect:init()
    local IsNull = HelperFunc.IsNull
    if IsNull(self.renderer) then
        local tmp = Util.Lua_GameObjectFindComponent(self.roleGo, "Animation", "MeshRenderer");
        if not tmp then
            self.renderer = tmp; --"Attachs/BarHp"
            self.mbp = ShaderEffectManager.getMaterialPropertyBlock();
        end
    end

    self:clear();
end


function EntityEffect:onUpdate(deltaTime)

    self:updateFlashEffect(deltaTime);

end

function EntityEffect:clear()

    local IsNull = HelperFunc.IsNull
    --if (not self.flashEffect) or IsNull(self.mbp) or IsNull(self.renderer) then
    --    return
    --end

    if IsNull(self.mbp) or IsNull(self.renderer) then
        return
    end

    self.mbp:SetFloat('_FlashTime', 1);--original color
    self.flashEffect = false;

    self.mbp:SetColor('_HitFlashColor', ShaderEffectManager.getHitFlashColor());
    self.renderer:SetPropertyBlock(self.mbp);

end